package czx.wt.common.response;

import java.io.Serializable;
import java.util.List;

/**
 * @program: spring-jwt
 * @description: 分页工具类
 * @author: ChenZhiXiang
 * @create: 2019-06-27 21:53
 **/
public class Pager<T> implements Serializable {

    private static final long serialVersionUID = -7423552189615226204L;


    //每页的记录数
    private Integer pageSize;
    //当前第几页
    private Integer currentPage;
    //总页数
    private Integer totalPage;
    //总记录数
    private Integer totalRecord;


    //是否有上一页
    private boolean lastPage;


    private boolean nextpage;


    //数据集合
    private List<T> dataList;


    public Pager() {
    }


    public Pager(int pageNum,int pageSize,List<T> data){
        if (data == null){
            return;
        }
//总记录数
        this.totalRecord = data.size();
//每页显示多少记录数
        this.pageSize = pageSize;
//获取总页数
        if (totalRecord%pageSize != 0){
            this.totalPage = totalRecord/pageSize + 1;
        }else {
            this.totalPage = totalRecord/pageSize;
        }
//当前第几页数据
        if (this.totalPage < pageNum){
            this.currentPage = totalPage;
        }else if (pageNum <= 0){
            this.currentPage = 1;
        }else {
            this.currentPage = pageNum;
        }


//起始索引
        int fromIndex = (this.currentPage - 1) * pageSize;
//结束索引
        int toIndex;
        if (this.pageSize * this.currentPage > this.totalRecord){
            toIndex = this.totalRecord;
        }else {
            toIndex = this.pageSize * this.currentPage;
        }


//上一页
        if (this.currentPage <= 1){
            lastPage = false;
        }else {
            lastPage = true;
        }

//下一页
        if (this.currentPage.equals(this.totalPage)){
            nextpage = false;
        }else {
            nextpage = true;
        }

        this.dataList = data.subList(fromIndex,toIndex);
    }


    public Pager(Integer pageSize, Integer currentPage,
                 Integer totalPage, Integer totalRecord, List<T> dataList) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalPage = totalPage;
        this.totalRecord = totalRecord;
        this.dataList = dataList;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(Integer totalRecord) {
        this.totalRecord = totalRecord;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    public void setLastPage(boolean lastPage) {
        this.lastPage = lastPage;
    }

    public boolean isNextpage() {
        return nextpage;
    }

    public void setNextpage(boolean nextpage) {
        this.nextpage = nextpage;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }
}
